﻿// Copyright (c) ZhangPeihang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using SuperDog;
using System.Text;
using System.Xml;

namespace LaserMark.SuperDog
{
    public class DogService : IDogService
    {
        private Dog _dog;

        private const string key =
               "UJUt1ieVfjp70yBWe1CcFN8JR4kAHw1DS/x3i6xGwyZeEMYwJ4VsLLilTSNqJG3Jrh9ofOd+w9atS5DQ" +
               "y/ZVXEabWOZ5sdj2QXwhHhkn0bFhnN2gxOaqOGonWgNZttTgJobz5fNIpStewabBTI7LAqdM4W5tGTr/" +
               "RS7RXaXBJsmFUmRKHJqciTXdaV0ruAUWSniB95aM4dcMdGkkVitbzmcV4CAaSLYBDH/l46LyNVHPNKxj" +
               "qOjFGMiDqVZdNGhm5rm6lpV8nooK0dhU7GRNca1gOSfBi96T3V2VeFqiTtMTP5uxrIRLFTYJLf3x0Z+z" +
               "SiHVVvINKenNAaKYXcx/1rXoNkm2Mc6P6xa92kMJrDIIB3MEOezEtXO5Or55iyAvf0wKZts2BiUp9KZH" +
               "yNw+H6phY/qEbjU3hNe6mYnpXastgmm8FMjYcp6JaH0y+u48sejwdF3G+Z/7Nsn+EKBIppl/3STuZV5m" +
               "j1rGgaDU4Cco5cK87ml1UdNDA5ytEyTX8WfsJjjIJbGv/YZyI2DziOyoyMHFvlS2gMp3DZX8OnmEtEt0" +
               "si5dQkscA41URH/cvJF+4ZaPkJqtnahrO0PEggvApNOLU8DgVRvK2KIeL7nmxXd3CTZCBFoU8rAKvljA" +
               "nGBCwMUDs0juEJwZafVLaxko6HiYPk3l8jUqEKkRoWT5eqiDKL8ECuhvD/i5Hfx/y2hHURbTiyVjVdqS" +
               "lAccpq2iXGnmIPplDDGNXsLgx6Yc99b5hdlVL60nGKdzE9Kot/fYOTq0vQsqtysEOZU0/KSMyjiACMUB" +
               "UD3cHHvJUecKl7jQdxHdPxY15XGH231+nqFta32HKn59n4dZqa7BwCQWmep9HUqrhE8/Zl3u9XC3WA8w" +
               "LjfyAiQkRikxlY0R6F9tbV29Jxcc2Y7Wiz74NXtQlZHgLieTBZLN9GEilWeaT8zPNSzAZFcfGStZdKcK" +
               "XzS9hqReelkWwJv1OUUpeg==";

        public DogService()
        {
            _dog = new Dog(DogFeature.FromFeature(1));
        }

        public bool IsLogin
        {
            get { return _dog.IsLoggedIn(); }
        }

        public Task<bool> DogLogin(string code)
        {
            var status = _dog.Login(key);
            return Task.FromResult(status == DogStatus.StatusOk);
        }

        public Task<bool> DogLogout()
        {
            _dog.Logout();
            return Task.FromResult(true);
        }

        public Task<DateTime> GetTime()
        {
            DateTime time = default;
            _dog.GetTime(ref time);

            return Task.FromResult(time);
        }

        public Task<DateTime> GetExpDate()
        {
            string format =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                    "<dogformat root=\"dog_info\">" +
                    "    <feature>" +
                    "        <attribute name=\"id\" />" +
                    "        <element name=\"license\" />" +
                    "        <dog>" +
                    "          <attribute name=\"id\" />" +
                    "          <attribute name=\"type\" />" +
                    "        </dog>" +
                    "    </feature>" +
                    "</dogformat>" +
                    "";
            string info = "";
            _dog.GetSessionInfo(format, ref info);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(info);
            var nodelist = doc.GetElementsByTagName("exp_date");
            var cc = nodelist[0]!.InnerText;
            DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1)); // 当地时区
            DateTime dt = startTime.AddSeconds(Convert.ToInt64(cc));
            return Task.FromResult(dt);
        }

        public Task<string> GetSn()
        {
            var file = _dog.GetFile(1);
            int size = 0;
            file.FileSize(ref size);

            var bytes = new byte[size];
            file.Read(bytes, 0, size);

            var str = Encoding.UTF8.GetString(bytes);

            return Task.FromResult(str);
        }

        public Task<string> GetMac()
        {
            var file = _dog.GetFile(2);
            int size = 0;
            file.FileSize(ref size);

            var bytes = new byte[size];
            file.Read(bytes, 0, size);

            var str = Encoding.UTF8.GetString(bytes);

            return Task.FromResult(str);
        }
    }
}
